package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
   TextView tvContentHtml;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvContentHtml=findViewById(R.id.text_html);
        HtmlImageGetter htmlImageGetter = new HtmlImageGetter();
        String source="<img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com" +
                "/uploads/20200326/0589f5b87f126b1c4675e826274f691b.jpg\" " +
                "alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com" +
                "/uploads/20200326/60a5e3d9fd4740eb129ea0e538b0a7ee.jpg\" alt=\"\" />" +
                "<img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/bc066e198db8b90f791125b532c7a8b2.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/8a98671539ad84db971f127c79d69e15.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/db7565dfa99525b8ea9d20d1daadc49f.jpg\" alt=\"\" />";



        Spanned spanned = Html.fromHtml(source, htmlImageGetter, null);
       tvContentHtml.setText(spanned);
        tvContentHtml.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动

    }


    /**
     * 重写图片加载接口
     *
     * @author Ruffian
     * @date 2016年1月15日
     *
     */
    class HtmlImageGetter implements Html.ImageGetter {

        /**
         * 获取图片
         */
        @Override
        public Drawable getDrawable(String source) {
            LevelListDrawable d = new LevelListDrawable();
            Drawable empty = getResources().getDrawable(
                    R.mipmap.ic_launcher_round);
            d.addLevel(0, 0, empty);
            d.setBounds(0, 0, DensityUtil.getScreenWidthDp(MainActivity.this),
                    empty.getIntrinsicHeight());
            new HtmlImageGetter.LoadImage().execute(source, d);
            return d;
        }

        /**
         * 异步下载图片类
         *
         * @author Ruffian
         * @date 2016年1月15日
         *
         */
        class LoadImage extends AsyncTask<Object, Void, Bitmap> {

            private LevelListDrawable mDrawable;

            @Override
            protected Bitmap doInBackground(Object... params) {
                String source = (String) params[0];
                mDrawable = (LevelListDrawable) params[1];
                try {
                    InputStream is = new URL(source).openStream();
                    return BitmapFactory.decodeStream(is);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            /**
             * 图片下载完成后执行
             */
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    BitmapDrawable d = new BitmapDrawable(bitmap);
                    mDrawable.addLevel(1, 1, d);
                    /**
                     * 适配图片大小 <br/>
                     * 默认大小：bitmap.getWidth(), bitmap.getHeight()<br/>
                     * 适配屏幕：getDrawableAdapter
                     */
                    mDrawable = getDrawableAdapter(MainActivity.this, mDrawable,
                            bitmap.getWidth(), bitmap.getHeight());

                    // mDrawable.setBounds(0, 0, bitmap.getWidth(),
                    // bitmap.getHeight());

                    mDrawable.setLevel(1);

                    /**
                     * 图片下载完成之后重新赋值textView<br/>
                     * mtvActNewsContent:我项目中使用的textView
                     *
                     */
                   tvContentHtml.invalidate();
                    CharSequence t =  tvContentHtml.getText();
               tvContentHtml.setText(t);

                }
            }

            /**
             * 加载网络图片,适配大小
             *
             * @param context
             * @param drawable
             * @param oldWidth
             * @param oldHeight
             * @return
             * @author Ruffian
             * @date 2016年1月15日
             */
            public LevelListDrawable getDrawableAdapter(Context context,
                                                        LevelListDrawable drawable, int oldWidth, int oldHeight) {
                LevelListDrawable newDrawable = drawable;
                long newHeight = 0;// 未知数
                int newWidth = DensityUtil.getScreenWidthDp(MainActivity.this);// 默认屏幕宽
                newHeight = (newWidth * oldHeight) / oldWidth;
                // LogUtils.w("oldWidth:" + oldWidth + "oldHeight:" +
                // oldHeight);
                // LogUtils.w("newHeight:" + newHeight + "newWidth:" +
                // newWidth);
                newDrawable.setBounds(0, 0, newWidth, (int) newHeight);
                return newDrawable;
            }
        }

    }
}
